package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.marriage.CrossGroupMarriageMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.C_PUBLISH_MARRIAGE, desc = "发布跨服联姻")
public class CrossMarriagePublishCmd extends CrossCommand {
    private long userId;
    private MarriageInfo marriageInfo;
    private BigInteger limitEarnSpeed = BigInteger.ZERO;
    private long appointUserId;
    private long partnerServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageApplyPublishCrossReqMsg reqMsg = CrossMarriageProto.CrossMarriageApplyPublishCrossReqMsg.parseFrom(packet.getBytes());
        marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
        String value = reqMsg.getLimitEarnSpeed();
        if(!StringUtils.isNullOrEmpty(value)){
            limitEarnSpeed = new BigInteger(value);
        }
        appointUserId = reqMsg.getAppointUserId();
        partnerServerId = reqMsg.getPartnerServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(appointUserId > 0){
            ret = CrossGroupMarriageMgr.publishAppointMarriage(serverId, marriageInfo, appointUserId, partnerServerId, limitEarnSpeed);
            if(0 != ret) {
                CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.newBuilder();
                respMsg.setRet(ret);
                respMsg.setChildUid(marriageInfo.getChildUid());
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_MARRIAGE_PUBLISH_RESP, respMsg);
                MessageHelper.sendPacket(serverId, userId, pbMsg);
            }
        }else {
            long publishTime = System.currentTimeMillis() / 1000;
            marriageInfo.setLimitEarnSpeed(limitEarnSpeed);
            marriageInfo.setPublishTime(publishTime);
            marriageInfo.setAppointUserId(appointUserId);
            marriageInfo.setPartnerServerId(partnerServerId);
            ret = CrossGroupMarriageMgr.publishServerMarriage(serverId, marriageInfo);
            CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyPublishCrossRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setChildUid(marriageInfo.getChildUid());
            respMsg.setLimitEarnSpeed(limitEarnSpeed.toString());
            respMsg.setAppointUserId(appointUserId);
            respMsg.setPartnerServerId(partnerServerId);
            respMsg.setPublishTime(publishTime);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_MARRIAGE_PUBLISH_RESP, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }
}
